<template>
  <view v-if="showModal" class="modal">
    <view class="modal-content">
      <view class="modal-header">
        <text class="modal-title">{{ title }}</text>
      </view>
      <view class="modal-body">
        <input type="text" v-model="internalProduct.code" placeholder="商品编号" class="input-field-rounded" />
        <input type="text" v-model="internalProduct.name" placeholder="商品名称" class="input-field-rounded" />
        <input type="text" v-model="internalProduct.specification" placeholder="商品规格" class="input-field-rounded" />
        <input type="text" v-model="internalProduct.costPrice" placeholder="成本价" class="input-field-rounded" />
        <input type="text" v-model="internalProduct.salePrice" placeholder="销售价" class="input-field-rounded" />
        <picker mode="selector" :range="statusOptions" @change="onProductStatusChange">
          <view class="picker">{{ internalProduct.status }}</view>
        </picker>
      </view>
      <view class="modal-footer">
        <button @click="submitProduct" class="action-button">提交</button>
        <button @click="hideModal" class="action-button">取消</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue';

// Define props
const props = defineProps({
  show: Boolean,
  title: String,
  product: Object
});

// Define emits
const emit = defineEmits(['update:show', 'submit']);

// Internal state
const showModal = ref(props.show);
const internalProduct = ref({ ...props.product });

// Status options for the picker
const statusOptions = ['在售', '缺货', '停产'];

// Handle status change
const onProductStatusChange = (event) => {
  internalProduct.value.status = event.detail.value;
};

// Submit product
const submitProduct = () => {
  emit('submit', internalProduct.value);
  hideModal();
};

// Hide modal
const hideModal = () => {
  showModal.value = false;
  emit('update:show', false);
};

// Watch for changes in props
watch(() => props.show, (newVal) => {
  showModal.value = newVal;
});
</script>

<style scoped>
.modal {
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

.modal-content {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 90%;
  max-width: 500px; /* 限制弹窗的最大宽度 */
}

.modal-header {
  border-bottom: 1px solid #eaeaea;
  padding-bottom: 10px;
  margin-bottom: 15px;
}

.modal-title {
  font-size: 18px;
  color: #333;
}

.modal-body {
  margin-bottom: 15px;
}

.input-field-rounded {
  width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  font-size: 14px;
}

.picker {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  margin-bottom: 10px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
}

.action-button {
  padding: 10px 20px;
  margin-left: 10px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: white;
  cursor: pointer;
  font-size: 14px;
}

.action-button:hover {
  background-color: #0056b3;
}

.action-button:active {
  transform: translateY(2px);
}
</style>
